﻿<%@ WebHandler Language="C#" Class="Access" %>

using System;
using System.Web;
using System.Collections.Generic;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.Site;
using Company.StudyClub.BLL.Common;
using System.Text;

public class Access : IHttpHandler {

    private HttpHelper helper = new HttpHelper();
    
    public void ProcessRequest (HttpContext context) {

        HttpResponse response = context.Response;
        
        switch (helper.GetString("type"))
        {
            case "addrole":
                AddRole(response);
                break;
            case "getrole":
                GetRole(response);
                break;
            case "editrole":
                EditRole(response);
                break;
            case "saverolefunctions":
                SaveRoleFunctions(response);
                break;
            case "roleusersnum":
                GetTotalRoleUsers(response);
                break;
            case "roleusers":
                GetRoleUsers(response);
                break;
            case "deleteroleusers":
                DeleteRoleUsers(response);
                break;
            case "addroleusers":
                AddRoleUsers(response);
                break;
            case "getuserfunctions":
                GetUserFunctions(response);
                break;
        }
    }

    private void AddRole(HttpResponse response)
    {
        Site_Roles model = new Site_Roles();
        model.RoleName = helper.GetString("rolename");
        model.Description = helper.GetString("desc");
        model.CreateTime = DateTime.Now;
        RolesBLL objBLL = new RolesBLL();
        if (objBLL.Add(model) > 0)
        {
            response.Write("1");
        }
        else
        {
            response.Write("0");
        }
    }

    private void GetRole(HttpResponse response)
    {
        Site_Roles model = new Site_Roles();
        RolesBLL objBLL = new RolesBLL();

        model = objBLL.GetModel(helper.GetInt("roleid"));

        if (model != null)
        {
            response.Write("{\"roleid\":\"" + model.RoleID + "\",\"rolename\":\"" + model.RoleName + "\",\"description\":\"" + model.Description + "\"}");
        }
        else
        {
            response.Write("{}");
        }
    }

    private void EditRole(HttpResponse response)
    {
        Site_Roles model = new Site_Roles();
        model.RoleName = helper.GetString("rolename");
        model.Description = helper.GetString("desc");
        model.RoleID = helper.GetInt("roleid");       
        RolesBLL objBLL = new RolesBLL();
        objBLL.Update(model);

        response.Write("1");
    }

    private void SaveRoleFunctions(HttpResponse response)
    {
        string[] functionList = helper.GetString("fids").Split(',');
        int roleid = helper.GetInt("roleid");
        
        RoleFunctionsBLL objBLL = new RoleFunctionsBLL();
        objBLL.Delete(roleid, 0);
        try
        {
            for (int i = 0; i < functionList.Length; i++)
            {
                if (functionList[i].Trim() == "")
                    continue;

                int functionID = Convert.ToInt32(functionList[i].Trim());

               
                Site_RoleFunctions model = new Site_RoleFunctions();
                model.RoleID = roleid;
                model.FunctionID = functionID;
                objBLL.Add(model);
            }

            response.Write("1");
        }
        catch(Exception ex)
        {
            response.Write(HttpContext.Current.Server.HtmlEncode(ex.ToString()));
        }
    }
    
    private void GetTotalRoleUsers(HttpResponse response)
    {
        RoleUsersBLL objBLL = new RoleUsersBLL();

        response.Write(objBLL.GetTotalUsersByRoleID(helper.GetInt("roleid")).ToString());
    }
    
    private void GetRoleUsers(HttpResponse response)
    {
        RoleUsersBLL objBLL = new RoleUsersBLL();
        
        List<User_Info> objList = objBLL.GetUsersByRoleID(helper.GetInt("pageindex"), helper.GetInt("pagesize"), helper.GetInt("roleid"));

        StringBuilder objSB = new StringBuilder();

        objSB.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            User_Info model = (User_Info)objList[i];
            objSB.Append("{\"uid\":\"");
            objSB.Append(model.UserID);
            objSB.Append("\",\"account\":\"");
            objSB.Append(model.Account);
            objSB.Append("\",\"nickname\":\"");
            objSB.Append(model.NickName);
            objSB.Append("\",\"realname\":\"");
            objSB.Append(model.RealName);
            objSB.Append("\",\"email\":\"");
            objSB.Append(model.Email);
            objSB.Append("\",\"row\":\"");
            objSB.Append(model.RowNumber);
            objSB.Append("\"}");

            if (i != objList.Count - 1)
                objSB.Append(",");
        }
        objSB.Append("]");

        response.Write(objSB.ToString());
    }

    private void DeleteRoleUsers(HttpResponse response)
    {
        try
        {
            string[] uids = helper.GetString("uids").Split(',');
            int roleId = helper.GetInt("roleid");
            for (int i = 0; i < uids.Length; i++)
            {
                if (uids[i] == string.Empty)
                    continue;

                RoleUsersBLL objBLL = new RoleUsersBLL();
                objBLL.Delete(roleId, int.Parse(uids[i]));
            }
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }
    }

    private void AddRoleUsers(HttpResponse response)
    {
        try
        {
            string[] uids = helper.GetString("uids").Split(',');
            int roleId = helper.GetInt("roleid");
            for (int i = 0; i < uids.Length; i++)
            {
                if (uids[i] == string.Empty)
                    continue;
                
                int userid = int.Parse(uids[i]);     
                
                RoleUsersBLL objBLL = new RoleUsersBLL();
                Site_RoleUsers model = objBLL.GetModel(roleId, userid);

                if (model == null)
                {
                    model = new Site_RoleUsers();
                    model.UserID = userid;
                    model.RoleID = roleId;
                    objBLL.Add(model);
                }
            }
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }
    }

    private void GetUserFunctions(HttpResponse response)
    {
        FunctionBLL objFunction = new FunctionBLL();
        List<Site_Functions> functionList = objFunction.GetFunctionsByUserId(helper.CurrentUser.UserID);

        StringBuilder sb = new StringBuilder();
        sb.Append("[");

        int currentGroupID = 0;

        for (int i = 0; i < functionList.Count; i++)
        {
            Site_Functions model = (Site_Functions)functionList[i];

            if (model.GroupID == currentGroupID && currentGroupID != 0)
            {
                sb.Append(",");
            }
            
            if (model.GroupID != currentGroupID)
            {
                if (currentGroupID != 0)
                {
                    sb.Append("]},");
                }

                sb.Append("{\"gname\":\"");
                sb.Append(model.GroupName);
                sb.Append("\",\"gid\":\"");
                sb.Append(model.GroupID);
                sb.Append("\",\"functions\":[");
                
                currentGroupID = model.GroupID;
            }          

            sb.Append("{\"fname\":\"");
            sb.Append(model.FunctionName);
            sb.Append("\",\"url\":\"");
            sb.Append(model.EnterUrl);
            sb.Append("\",\"fid\":\"");
            sb.Append(model.FunctionID);
            sb.Append("\"}");

            if (i == functionList.Count - 1)
            {
                sb.Append("]}");
            }            
        }
        sb.Append("]");

        response.Write(sb.ToString());
    }

    public bool IsReusable
    {
        get {
            return false;
        }
    }

}